<?php
namespace jy_weishop;
defined('BY_JYA') or exit('error');
/*
	购物卡
*/
class goodscard extends \plugin{
	public function getcard($id=null,$gettype=1,$senduid=0){
		if(!$id){
			exi('购物卡不存在，请重新选择购物卡','error');
		}
		$card = pdo_get('jy_weishop_goodscard',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		$getallnum = pdo_count('jy_weishop_goodscard_getlog',array('uniacid'=>$_SESSION['uniacid'],'cardid'=>$id));
		if($card['t'] <= $getallnum && $card['t']>0){
			exi('不能再领购物卡了，已经上限了!','error');
		}
		$getnum = pdo_count('jy_weishop_goodscard_getlog',array('uniacid'=>$_SESSION['uniacid'],'cardid'=>$id,'mid'=>$_SESSION['uid']));
		if($getnum && $card['getmax'] && $card['getmax'] <= $getnum){
			exi( '不能再领购物卡了，已经上限了!','error');
		}
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'cardid'=>$id,
			'gettype'=>$gettype,
			'money'=>$card['money'],
			'lastmoney'=>$card['money'],
			'senduid'=>$senduid,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_weishop_goodscard_getlog',$data);
		return true;
	}
	public function setlog($params){
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'ordersn'=>$params['ordersn'],
			'getid'=>$params['getid'],
			'cardid'=>$params['cardid'],
			'fee'=>$params['fee'],
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_weishop_goodscard_uselog',$data);
		return true;
	}
	public function getorderfee($goodsprice='',$level_de_price='',$orders=''){
		$cards=pdo_getall('jy_weishop_goodscard_getlog',array('money'=>0,'isuse'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		foreach($cards as $key=>&$card){
			$money=pdo_getcolumn('jy_weishop_goodscard',array('id'=>$card['cardid'],'uniacid'=>$_SESSION['uniacid']),'money');
			if($money>0){
				pdo_update('jy_weishop_goodscard_getlog',array('money'=>$money,'lastmoney'=>$money),array('id'=>$card['id'],'mid'=>$_SESSION['uid']));
			}
		}
		$lastmoney=0;
		$newcards=$this->getmycard_ing($goodsprice,$level_de_price,$orders);
		foreach($newcards as $cv){
			$lastmoney +=$cv['lastmoney'];
		}
		$lastmoney=price($lastmoney,'int');
		return $lastmoney;
	}
	public function getmycard_ing($goodsprice,$level_de_price='',$orders=''){
		//整理购物卡数据，先获取用户所有待使用购物卡，再从中筛选出未过期的购物卡且本次下单可用的购物卡！
		$cards=$this->getmycard();
		$lastmoney=0;
		if($cards){
			$newcards=array();
			foreach($cards as $key=>$card){
				$card_detail=pdo_get('jy_weishop_goodscard',array('id'=>$card['cardid'],'uniacid'=>$_SESSION['uniacid']));
				if(!$card_detail){
					unset($cards[$key]);
					continue;
				}
				//判断使用限制
				if($card_detail['limitusetype']){
					switch($card_detail['limitusetype']){
						case 1://不可与促销优惠同时使用
							
						case 2://不可与会员折扣同时使用
							if($level_de_price){
								unset($cards[$key]);
								continue;
							}
						case 3://不可与促销、折扣同时使用
							if($level_de_price){
								unset($cards[$key]);
								continue;
							}
					}
				}
				//判断有效期
				if($card_detail['timelimittype']==1){
					//有效期类型为领取后若干天内使用
					$days=(TIMESTAMP-$card['createtime'])/86400;
					if($card_detail['timedays']<$days){
						//已过期
						unset($cards[$key]);
						continue;
					}
				}elseif($card_detail['timelimittype']==2){
					$card_detail['limittimes']=json_decode($card_detail['limittimes'],true);
					if(TIMESTAMP<$card_detail['limittimes']['start'] || TIMESTAMP>$card_detail['limittimes']['end']){
						//不在有效期内
						unset($cards[$key]);
						continue;
					}
				}
				//判断是否当前可用
				if($card_detail['limitgoodtype'] && $card_detail['limitgoodids']){
						$card_detail['limitgoodids']=str_array($card_detail['limitgoodids']);
						$check=false;
						if($orders){
							
							foreach($orders as $g){
								if(in_array($g['goodsid'],$card_detail['limitgoodids'])){
									$check=true;
								}
							}
						}
						if(!$check){
							//不可用
							unset($cards[$key]);
							continue;
						}
						
				}elseif($card_detail['limitgoodcatetype'] && $card_detail['limitgoodcateids']){
						$card_detail['limitgoodcateids']=str_array($card_detail['limitgoodcateids']);
						$check=false;
						if($orders){
							foreach($orders as $g){
								if(in_array($g['pcate'],$card_detail['limitgoodcateids']) || in_array($g['ccate'],$card_detail['limitgoodcateids'])){
									$check=true;
								}
							}
						}
						if(!$check){
							//不可用
							unset($cards[$key]);
							continue;
						}
				}
				$newcards[]=$card;
			}
		}
		if(!empty($newcards)){
			paixu($newcards,'lastmoney','desc');
		}
		return $newcards;
	}
	public function getmycard($isuse='',$type='',$past=''){
		$p=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
		if($isuse==1){
			$p=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'isuse'=>1);
		}else{
			$p=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'islock'=>0,'isuse'=>0);
		}
		$return['isuse'] = $isuse ? $isuse :0;
		$list=pdo_getall('jy_weishop_goodscard_getlog',$p);
		foreach($list as &$v){
			$v['card']=$this->intcard($v['cardid'],$v['createtime']);
		}
		return $list;
	}
	public function getdetail($id,$cardid=false){
		if($cardid){
			$item['cardid']=$id;
		}else{
			$p=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'id'=>$id);
			$item=pdo_get('jy_weishop_goodscard_getlog',$p);
		}
		$item['card']=$this->intcard($item['cardid'],$item['createtime']);
		$item['card']['limitgoodids']= explode(';', $item['card']['limitgoodids']);
		$item['card']['limitgoodids']= substr(implode(',', $item['card']['limitgoodids']),1,strlen(implode(',', $item['card']['limitgoodids']))-2);
		$item['card']['limitgoodcateids']= explode(';', $item['card']['limitgoodcateids']);
		$item['card']['limitgoodcateids']= substr(implode(',', $item['card']['limitgoodcateids']),1,strlen(implode(',', $item['card']['limitgoodcateids']))-2);
		if ($item['card']['limitgoodtype'] != 0) {
			if (!empty($item['card']['limitgoodids'])) {
				$where = 'and id in(' . $item['card']['limitgoodids'] . ')';
			}
			$item['goods'] = pdo_fetchall('select `title` from ' . tablename('jy_weishop_goods') . ' where uniacid=:uniacid ' . $where, array(':uniacid' => $_SESSION['uniacid']), 'id');
		}
		if ($item['card']['limitgoodcatetype'] != 0) {
			if (!empty($item['card']['limitgoodcateids'])) {
				$where = 'and id in(' . $item['card']['limitgoodcateids'] . ')';
			}
			$item['group'] = pdo_fetchall('select `title`  from ' . tablename('jy_weishop_goods_group') . ' where uniacid=:uniacid   ' . $where, array(':uniacid' => $_SESSION['uniacid']), 'id');
		}
		return $item;
	}
	//整理一下券
	public function intcard($id,$createtime=0){
			$gettype=array('1'=>'充值兑换','2'=>'活动赠送','3'=>'朋友赠送');
			$card=pdo_get('jy_weishop_goodscard',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			if ($card['t'] >=1){
					$getallnum = pdo_count('jy_weishop_goodscard_getlog',array('uniacid'=>$_SESSION['uniacid'],'cardid'=>$id));
					if ($getallnum >= $card['t']){
						$card['last'] = 0;
						$card['isdisa'] = '1';
					}else{
						$totle = $card['t'];
						$card['lastratio'] = intval(($card['last'] / $totle) * 100);
					}
			}else{
					$card['last'] = 1;
					$card['lastratio'] = 100;
			}
			$card['gettypetitle']=$gettype[$card['gettype']];
			$card['rightdesc']='来自：'.$gettype[$card['gettype']];
			if($card['merchid']){
				$card['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$card['merchid']),'merchname');
			}
			$card['past']=0;
			if ($card['timelimittype'] == '0') {
				$card['color'] = 'blue';
				$card['time'] = '永久有效';
				$timetitle= '永久有效';
			}elseif($card['timelimittype']==1){
				$card['color'] = 'orange';
				$time=$createtime+$card['timedays']*24*3600;
				if(time()>$time){
					$card['past']=1;
				}
				$start=$createtime;
				$start=date('Y/m/d',$start);
				$time=date('Y/m/d',$time);
				$card['time']=$start.'至'.$time;
			}elseif($card['timelimittype']==2){
				$card['color'] = 'red';
				$card['limittimes']=json_decode($card['limittimes'],true);
				if(time()>$card['limittimes']['tend']){
					$card['past']=1;
				}
				$card['limittimes']['tstart']=date('Y/m/d',$card['limittimes']['tstart']);
				$card['limittimes']['tend']=date('Y/m/d',$card['limittimes']['tend']);
				$card['time']=$card['limittimes']['tstart'].'至'.$card['limittimes']['tend'];
			}
			$card['timetitle'] = $card['time'];
		return $card;
	}
	public function usecard($order){
		$cards=pdo_getall('jy_weishop_goodscard_getlog',array('lastmoney >'=>0,'isuse'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'','','lastmoney asc');
		$di_fee=$order['di_cardfee'];
		foreach($cards as $key=>&$card){
			if($di_fee<=0){
				break;
			}
			$order['cardid']=$card['cardid'];
			$order['getid']=$card['id'];
			if($card['lastmoney']>$di_fee){
				pdo_update('jy_weishop_goodscard_getlog',array('lastmoney -='=>$di_fee),array('id'=>$card['id'],'uniacid'=>$_SESSION['uniacid']));
				$order['fee']=$di_fee;
				$this->setlog($order);
				break;
			}else{
				pdo_update('jy_weishop_goodscard_getlog',array('lastmoney'=>0,'isuse'=>1),array('id'=>$card['id'],'uniacid'=>$_SESSION['uniacid']));
				$order['fee']=$card['lastmoney'];
				$this->setlog($order);
				$di_fee -=$card['lastmoney'];
			}
		}
		return true;
	}
	//退款或取消订单
	public function tuiorder($order){
		$uselogs=pdo_getall('jy_weishop_goodscard_uselog',array('ordersn'=>$order['ordersn'],'status <>'=>2,'mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
		foreach($uselogs as $item){
			if($item['fee']>0){
				pdo_update('jy_weishop_goodscard_getlog',array('lastmoney +='=>$item['fee'],'isuse'=>0),array('id'=>$item['getid'],'mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
		pdo_update('jy_weishop_goodscard_uselog',array('status'=>2),array('ordersn'=>$order['ordersn'],'mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
}